package com.ymatou.iapi.hunter.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.GetHunterSuperiorRequest;
import com.ymatou.iapi.hunter.service.GetHunterSuperiorCall;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.HunterCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.model.hunter.HunterFans;
import com.ymttest.database.model.hunter.HunterRelation;
import com.ymttest.database.model.hunter.HunterRelationTemp;
import com.ymttest.database.sqlwapper.hunter.HunterFansWrapper;
import com.ymttest.database.sqlwapper.hunter.HunterRelationTempWrapper;
import com.ymttest.database.sqlwapper.hunter.HunterRelationWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

public class Ts_GetHunterSuperior {
	
	private static GetHunterSuperiorRequest getHunterSuperiorRequest;
	private static GetHunterSuperiorCall getHunterSuperiorCall;
	
	HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询用户直属上级");
		
		String accountid1;
		try {
			accountid1 = AccountCallServiceV2.getAccountId(20238699);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					9000000), new BigDecimal(0));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		getHunterSuperiorRequest = new GetHunterSuperiorRequest();
		getHunterSuperiorCall = new GetHunterSuperiorCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHunterSuperiorCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify() {
		
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_001() {
		Logger.start(true, "绑定临时关系，查询用户上级");
		try {
			
			Logger.comment("前置数据准备开始");
			
			String weixinId = TestDataManager.randomStr(13);
			long hunterId = 1234L;
			
			HunterCallService.bindTempRelation(hunterId, weixinId);
			
			Logger.comment("前置数据准备结束");
			
			getHunterSuperiorRequest.setUserId(0);
			getHunterSuperiorRequest.setWeixinId(weixinId);
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelationTempWrapper hunterRelationTempWrapper = new HunterRelationTempWrapper();
			HunterRelationTemp hunterRelationTemp = hunterRelationTempWrapper.selectByPrimaryKey(weixinId);
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey(hunterId);
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterRelationTemp.getHunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					1, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					hunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					false, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterRelationTemp.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_002() {
		Logger.start(true, "购买普通商品，绑定永久关系，查询用户上级");
		try {
			
			Logger.comment("前置数据准备开始");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			
			// 创建订单
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();
			
			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrder.getOrderList().get(0).getOrderId());
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(3000);
			
			getHunterSuperiorRequest.setUserId(userId);
			getHunterSuperiorRequest.setWeixinId("");
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterFansWrapper hunterFansWrapper = new HunterFansWrapper();
			HunterFans hunterFans = hunterFansWrapper.selectByPrimaryKey((long)userId);
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey(hunterId);
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterFans.getHunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					2, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					hunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					false, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterFans.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_003() {
		Logger.start(true, "购买大礼包，成为推手，绑定永久关系，查询用户上级");
		try {
			
			Logger.comment("前置数据准备开始");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			
			// 创建订单
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();
			
			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrder.getOrderList().get(0).getOrderId());
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(3000);
			
			getHunterSuperiorRequest.setUserId(userId);
			getHunterSuperiorRequest.setWeixinId("");
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey((long)userId);
			
			HunterRelation parentHunterRelation = hunterRelationWrapper
					.selectByPrimaryKey(hunterRelation.getParent_hunter_id());
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterRelation.getParent_hunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					2, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					parentHunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					true, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterRelation.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_004() {
		Logger.start(true, "购买A的商品，绑定永久关系，然后购买B大礼包，成为推手，查询用户上级应该为A");
		try {
			
			Logger.comment("前置数据准备开始");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			
			// 购买A的商品，绑定永久关系
			long hunterIdA = 4321;
			TradingServiceResp placeOrderA = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterIdA, userId, 2);
			long tradingIdA = placeOrderA.getTradingId();
			BigDecimal totalPriceA = placeOrderA.getPayableAmount();
			
			Long thirdTradingIdA = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingIdA, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingIdA.toString(),
					totalPriceA.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrderA.getOrderList().get(0).getOrderId());
			
			//购买b的大礼包
			long hunterIdB = 1022;
			TradingServiceResp placeOrderB = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterIdB, userId, 1);
			long tradingIdB = placeOrderB.getTradingId();
			BigDecimal totalPriceB = placeOrderB.getPayableAmount();
			
			Long thirdTradingIdB = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingIdB, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingIdB.toString(),
					totalPriceB.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrderB.getOrderList().get(0).getOrderId());
			
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(5000);
			
			getHunterSuperiorRequest.setUserId(userId);
			getHunterSuperiorRequest.setWeixinId("");
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey(hunterIdA);
			
			HunterRelation hunterFans = hunterRelationWrapper.selectByPrimaryKey((long)userId);
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterFans.getParent_hunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					2, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					hunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					true, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterFans.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_005() {
		Logger.start(true, "通过A的邀请码，绑定永久关系，然后购买B大礼包，成为推手，查询用户上级应该为A");
		try {
			
			Logger.comment("前置数据准备开始");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			
			// 通过code，绑定永久关系
			
			long hunterIdA = 4321;
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey(hunterIdA);
			
			String code = hunterRelation.getCode();
			HunterCallService.bingRelationByHunterCode(userId, JUserCallService
					.GetUserInfo(userId).getLoginId(), code);
			
			//购买b的大礼包
			long hunterIdB = 1022;
			TradingServiceResp placeOrderB = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterIdB, userId, 1);
			long tradingIdB = placeOrderB.getTradingId();
			BigDecimal totalPriceB = placeOrderB.getPayableAmount();
			
			Long thirdTradingIdB = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingIdB, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingIdB.toString(),
					totalPriceB.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrderB.getOrderList().get(0).getOrderId());
			
			Logger.comment("前置数据准备结束");
			
			getHunterSuperiorRequest.setUserId(userId);
			getHunterSuperiorRequest.setWeixinId("");
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterFansWrapper hunterFansWrapper = new HunterFansWrapper();
			HunterFans hunterFans = hunterFansWrapper.selectByPrimaryKey((long)userId);
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterFans.getHunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					2, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					hunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					false, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterFans.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_006() {
		Logger.start(true, "通过A的邀请码，绑定永久关系，然后购买B商品，查询用户上级应该为A");
		try {
			
			Logger.comment("前置数据准备开始");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			
			// 通过code，绑定永久关系
			long hunterIdA = 4321;
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey(hunterIdA);
			
			String code = hunterRelation.getCode();
			HunterCallService.bingRelationByHunterCode(userId, JUserCallService
					.GetUserInfo(userId).getLoginId(), code);
			
			//购买b的大礼包
			long hunterIdB = 1022;
			TradingServiceResp placeOrderB = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterIdB, userId, 2);
			long tradingIdB = placeOrderB.getTradingId();
			BigDecimal totalPriceB = placeOrderB.getPayableAmount();
			
			Long thirdTradingIdB = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingIdB, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingIdB.toString(),
					totalPriceB.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrderB.getOrderList().get(0).getOrderId());
			
			Logger.comment("前置数据准备结束");
			
			getHunterSuperiorRequest.setUserId(userId);
			getHunterSuperiorRequest.setWeixinId("");
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterFansWrapper hunterFansWrapper = new HunterFansWrapper();
			HunterFans hunterFans = hunterFansWrapper.selectByPrimaryKey((long)userId);
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterFans.getHunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					2, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					hunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					false, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterFans.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSuperior_007() {
		Logger.start(true, "已禁用的推手，购买大礼包，成为正常推手，绑定新的永久关系，查询用户上级");
		try {
			
			Logger.comment("前置数据准备开始");
			
			int userId = 25194957;
			
			// 构建hunterRelation
			List<Long> partnerList = HunterCallService.buildHunterRelation(4,
					1, 0);

			long hunterId = partnerList.get(0);

			// 更改用户状态为废弃
			hunterRelationWrapper.updateStatusByHunterId((long)userId, 1);
			
			// 创建订单
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();
			
			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId);
			TradingSyncService.getSyncResult(placeOrder.getOrderList().get(0).getOrderId());
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(3000);
			
			getHunterSuperiorRequest.setUserId(userId);
			getHunterSuperiorRequest.setWeixinId("");
			
			getHunterSuperiorCall.setData(getHunterSuperiorRequest);
			getHunterSuperiorCall.callService();
			
			Logger.verifyEquals("success", getHunterSuperiorCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSuperiorCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSuperiorCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelationWrapper hunterRelationWrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = hunterRelationWrapper.selectByPrimaryKey((long)userId);
			
			HunterRelation parentHunterRelation = hunterRelationWrapper
					.selectByPrimaryKey(hunterRelation.getParent_hunter_id());
			
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getLong("hunterId"),
					hunterRelation.getParent_hunter_id(), "验证hunterId");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getInt("relationType"),
					2, "验证relationType");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getString("hunterName"),
					parentHunterRelation.getHunter_name(), "验证hunterName");
			Logger.verifyEquals(
					getHunterSuperiorCall.getResult().getBoolean("hunter"),
					true, "验证hunter");
			DataUtil.verifyDateEquals(getHunterSuperiorCall.getResult()
					.getString("addTime"), hunterRelation.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
